package cn.stylefeng.guns.sys.modular.system.controller;

import cn.stylefeng.guns.base.pojo.page.LayuiPageInfo;
import cn.stylefeng.guns.sys.core.listener.MtsxfExcelListener;
import cn.stylefeng.guns.sys.core.listener.YaoluBankFlowExcelListener;
import cn.stylefeng.guns.sys.core.util.DateUtil;
import cn.stylefeng.guns.sys.core.util.JsonResult;
import cn.stylefeng.guns.sys.modular.system.entity.BtyBrand;
import cn.stylefeng.guns.sys.modular.system.entity.BtyMtsxf;
import cn.stylefeng.guns.sys.modular.system.entity.YaoluBankFlow;
import cn.stylefeng.guns.sys.modular.system.mapper.BtyBankMapper;
import cn.stylefeng.guns.sys.modular.system.mapper.YaoluBankFlowMapper;
import cn.stylefeng.guns.sys.modular.system.model.params.BtyBankParam;
import cn.stylefeng.guns.sys.modular.system.model.params.BtyMtsxfParam;
import cn.stylefeng.guns.sys.modular.system.model.params.YaoluBankFlowParam;
import cn.stylefeng.guns.sys.modular.system.model.result.BtyBankResult;
import cn.stylefeng.guns.sys.modular.system.model.result.BtyMtsxfResult;
import cn.stylefeng.guns.sys.modular.system.model.result.YaoluBankFlowResult;
import cn.stylefeng.guns.sys.modular.system.service.YaoluBankFlowService;
import cn.stylefeng.roses.core.base.controller.BaseController;
import cn.stylefeng.roses.core.reqres.response.ResponseData;
import cn.stylefeng.roses.core.util.ToolUtil;
import com.alibaba.excel.EasyExcel;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.google.gson.JsonObject;
import com.google.gson.JsonParser;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.MediaType;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.client.RestTemplate;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.text.ParseException;
import java.util.List;


/**
 * 控制器
 *
 * @author liuy
 * @Date 2022-06-20 14:07:07
 */
@Controller
@RequestMapping("/yaoluBankFlow")
public class YaoluBankFlowController extends BaseController {

    private String PREFIX = "/modular/system/yaoluBankFlow";

    @Autowired
    private YaoluBankFlowService yaoluBankFlowService;
    @Resource
    private YaoluBankFlowMapper bankFlowMapper;
    @Resource
    private BtyBankMapper btyBankMapper;

    /**
     * 跳转到主页面
     *
     * @author liuy
     * @Date 2022-06-20
     */
    @RequestMapping("")
    public String index() {
        return PREFIX + "/yaoluBankFlow.html";
    }

    /**
     * 新增页面
     *
     * @author liuy
     * @Date 2022-06-20
     */
    @RequestMapping("/add")
    public String add() {
        return PREFIX + "/yaoluBankFlow_add.html";
    }

    /**
     * 编辑页面
     *
     * @author liuy
     * @Date 2022-06-20
     */
    @RequestMapping("/edit")
    public String edit() {
        return PREFIX + "/yaoluBankFlow_edit.html";
    }

    /**
     * 新增接口
     *
     * @author liuy
     * @Date 2022-06-20
     */
    @RequestMapping("/addItem")
    @ResponseBody
    public ResponseData addItem(YaoluBankFlowParam yaoluBankFlowParam) {
        this.yaoluBankFlowService.add(yaoluBankFlowParam);
        return ResponseData.success();
    }

    /**
     * 编辑接口
     *
     * @author liuy
     * @Date 2022-06-20
     */
    @RequestMapping("/editItem")
    @ResponseBody
    public ResponseData editItem(YaoluBankFlowParam yaoluBankFlowParam) {
        this.yaoluBankFlowService.update(yaoluBankFlowParam);
        return ResponseData.success();
    }

    /**
     * 删除接口
     *
     * @author liuy
     * @Date 2022-06-20
     */
    @RequestMapping("/delete")
    @ResponseBody
    public ResponseData delete(YaoluBankFlowParam yaoluBankFlowParam) {
        this.yaoluBankFlowService.delete(yaoluBankFlowParam);
        return ResponseData.success();
    }

    /**
     * 查看详情接口
     *
     * @author liuy
     * @Date 2022-06-20
     */
    @RequestMapping("/detail")
    @ResponseBody
    public ResponseData detail(YaoluBankFlowParam yaoluBankFlowParam) {
        YaoluBankFlow detail = this.yaoluBankFlowService.getById(yaoluBankFlowParam.getId());
        return ResponseData.success(detail);
    }

    /**
     * 查询列表
     *
     * @author liuy
     * @Date 2022-06-20
     */
    @ResponseBody
    @RequestMapping("/list")
    public LayuiPageInfo list(YaoluBankFlowParam yaoluBankFlowParam) {
        return this.yaoluBankFlowService.findPageBySpec(yaoluBankFlowParam);
    }

    /**
     * 导入excel
     */
    @RequestMapping("/import")
    @ResponseBody
    public Object addUsers(@RequestParam("file") MultipartFile file, YaoluBankFlowParam param) throws ParseException {
        JsonResult jsonResult = new JsonResult();
//        if(ToolUtil.isEmpty(param.getZdrq())){
//            jsonResult.setState(0);
//            jsonResult.setErrmsg("请先选择制单日期！");
//            return jsonResult;
//        }
        //导入之前删掉之前的数据
        //bankFlowMapper.delete(new QueryWrapper<>());

        try {
            YaoluBankFlowExcelListener basicExcelListener = new YaoluBankFlowExcelListener(yaoluBankFlowService);
            EasyExcel.read(file.getInputStream(), YaoluBankFlow.class, basicExcelListener).sheet(0).doRead();

        }catch (Exception e) {
            e.printStackTrace();
            jsonResult.setState(0);
            jsonResult.setErrmsg(e.getMessage());
            return jsonResult;
        }
        return true;
    }


    /**
     * 银行账收入凭证
     */
    @RequestMapping(value = "/yhzsr")
    @ResponseBody
    public ResponseData yhzsr(YaoluBankFlowParam param) throws ParseException {
        String beginTime = "";
        String endTime = "";
        String firstDay="";
        String lastDay="";
        String lastDays="";
        BtyBankParam bankParam=new BtyBankParam();
        if (ToolUtil.isNotEmpty(param.getTimeLimit())) {
            String[] split = param.getTimeLimit().split(" - ");
            beginTime = split[0].replace("-", "");
            endTime = split[1].replace("-", "");
            //获取beginTime所在月的第一天和最后一天
            firstDay = DateUtil.getMonthFirstDay(beginTime);
            //lastDay=DateUtil.getMonthLastDay(beginTime);
            lastDay = DateUtil.getMonthLastDays(endTime);
            lastDays = DateUtil.getLastDayByMonths(split[1]);
            param.setBeginTime(beginTime);
            param.setEndTime(endTime);
            param.setFirstDay(firstDay);
            param.setLastDay(lastDay);

            bankParam.setBeginTime(beginTime);
            bankParam.setEndTime(endTime);
            bankParam.setFirstDay(firstDay);
            bankParam.setLastDay(lastDay);
        }

            //导入成功之后按大区生成凭证
            BtyBankResult results=btyBankMapper.getSxfByDm(bankParam);

            BigDecimal zfbsxf=BigDecimal.ZERO;
            BigDecimal wxsxf=BigDecimal.ZERO;
            BigDecimal mdsxf=BigDecimal.ZERO;
            BigDecimal mtsxf=BigDecimal.ZERO;
            BigDecimal sxfsxf=BigDecimal.ZERO;
            //获取单店的支付宝手续费，微信手续费，买单手续费，美团手续费
            if(ToolUtil.isNotEmpty(results)){
                zfbsxf=results.getZfbsxf().setScale(2, BigDecimal.ROUND_HALF_UP);
                wxsxf=results.getWxsxf();
                mdsxf=results.getMdsxf();
                mtsxf=results.getMtsxf();
                sxfsxf=results.getSxfsxf();
            }

            //美团团购
            BigDecimal mttg=BigDecimal.ZERO;
            //美团买单
            BigDecimal mtmd=BigDecimal.ZERO;
            //饿了么
            BigDecimal elm=BigDecimal.ZERO;
            //美团外卖
            BigDecimal mtwm=BigDecimal.ZERO;

            List<YaoluBankFlowResult> list=yaoluBankFlowService.bankListByDm(param);
            for (YaoluBankFlowResult yybbResult : list) {
                String zy=yybbResult.getZy()==null?"":yybbResult.getZy();
                if(zy.contains("美团团购")){
                    mttg=mttg.add(yybbResult.getSr());
                } else if(zy.contains("美团买单")|zy.contains("超值优惠券包")){
                    mtmd=mtmd.add(yybbResult.getSr());
                }
                else if(zy.contains("美团外卖")){
                    mtwm=mtwm.add(yybbResult.getSr());
                }else if(zy.contains("饿了吗")|zy.contains("elm")){
                    elm=elm.add(yybbResult.getSr());
                }
            }

            String yaoma=list.get(0).getYaoma();
            String kh=list.get(0).getKh();
            String bh=list.get(0).getBh();

            int mttgFlag=mttg.compareTo(BigDecimal.ZERO);
            int mtmdFlag=mtmd.compareTo(BigDecimal.ZERO);
            int mtwmFlag=mtwm.compareTo(BigDecimal.ZERO);
            int elmFlag=elm.compareTo(BigDecimal.ZERO);

            int mdsxfFlag=mdsxf.compareTo(BigDecimal.ZERO);

            int zfbsxfFlag=zfbsxf.compareTo(BigDecimal.ZERO);
            int mtsxfFlag=mtsxf.compareTo(BigDecimal.ZERO);
            int wxsxfFlag=wxsxf.compareTo(BigDecimal.ZERO);


            RestTemplate restTemplate = new RestTemplate();
            HttpHeaders headers = new HttpHeaders();
            MediaType type = MediaType.parseMediaType("application/json; charset=UTF-8");
            headers.setContentType(type);
            headers.add("trantype", "code"); // 档案翻译方式，枚举值为：编码请录入 code， 名称请录入 name， 主键请录入 pk
            headers.add("system", "001"); // 系统编码
            headers.add("usercode", "15253172037"); // 用户
            headers.add("password", "514576f58a89767b46c1b44767a08f77");
            headers.add("Accept", MediaType.APPLICATION_JSON.toString());
            BigDecimal mtDf=mtsxf.add(mttg);
            BigDecimal mdDf=mdsxf.add(mtmd);

            StringBuilder sb=new StringBuilder();
            sb.append("{\n" +
                    "    \"voucher\":[\n" +
                    "        {\n" +
                    "            \"details\":[");

            //美团团购金额大于0的话，生成美团团购的凭证
            if(mttgFlag==1){
                if(mtsxfFlag==1) {
                    sb.append("                {\n" +
                            "                    \"ass\":[\n" +
                            "                        {\n" +
                            "                            \"checktypecode\":\"2\",\n" +
                            "                            \"checkvaluecode\":'" + yaoma+ "'\n" +
                            "                        }\n" +
                            "                    ],\n" +
                            "                    \"creditamount\":\"\",\n" +
                            "                    \"debitamount\":\"" + mtsxf + "\",\n" +
                            "                    \"explanation\":\"美团服务费\",\n" +
                            "                    \"localcreditamount\":\"\",\n" +
                            "                    \"localdebitamount\":\"\",\n" +
                            "                    \"pk_accsubj\":\"660120\",\n" +
                            "                    \"pk_currtype\":\"CNY\"\n" +
                            "                },");
                }
                sb.append("{\n" +
                        "                    \"ass\":[\n" +
                        "                        {\n" +
                        "                            \"checktypecode\":\"2\",\n" +
                        "                            \"checkvaluecode\":'"+yaoma+"'\n" +
                        "                        },\n" +
                        "                        {\n" +
                        "                            \"checktypecode\":\"96\",\n" +
                        "                            \"checkvaluecode\":"+kh+"\n" +
                        "                        }\n" +
                        "                    ],\n" +
                        "                    \"creditamount\":\"\",\n" +
                        "                    \"debitamount\":\""+mttg+"\",\n" +
                        "                    \"explanation\":\"收美团\",\n" +
                        "                    \"localcreditamount\":\"\",\n" +
                        "                    \"localdebitamount\":\"\",\n" +
                        "                    \"pk_accsubj\":\"100202\",\n" +
                        "                    \"pk_currtype\":\"CNY\"\n" +
                        "                },"+
                        "{\n" +
                        "\t\t\t\t\"ass\":[\n" +
                        "                        {\n" +
                        "                            \"checktypecode\":\"2\",\n" +
                        "                            \"checkvaluecode\":'"+yaoma+"'\n" +
                        "                        },\n" +
                        "\t\t\t\t\t\t{\n" +
                        "                            \"checktypecode\":\"7\",\n" +
                        "                            \"checkvaluecode\":\"0031\"\n" + //美团团购编号
                        "                        }\n" +
                        "                    ],\n" +
                        "                    \"creditamount\":\""+mtDf+"\",\n" +
                        "                    \"creditquantity\":\"\",\n" +
                        "                    \"debitamount\":\"\",\n" +
                        "                    \"debitquantity\":\"\",\n" +
                        "                    \"detailindex\":\"\",\n" +
                        "                    \"excrate1\":\"\",\n" +
                        "                    \"explanation\":\"收美团\",\n" +
                        "                    \"localcreditamount\":\"\",\n" +
                        "                    \"localdebitamount\":\"\",\n" +
                        "                    \"pk_accsubj\":\"112205\",\n" +
                        "                    \"pk_currtype\":\"CNY\",\n" +
                        "                    \"price\":\"\"\n" +
                        "                },\n" );
            }
            //饿了么
            if(elmFlag==1){
                sb.append("                {\n" +
                        "                    \"ass\":[\n" +
                        "                        {\n" +
                        "                            \"checktypecode\":\"2\",\n" +
                        "                            \"checkvaluecode\":'"+yaoma+"'\n" +
                        "                        },\n" +
                        "                        {\n" +
                        "                            \"checktypecode\":\"96\",\n" +
                        "                            \"checkvaluecode\":"+kh+"\n" +
                        "                        }\n" +
                        "                    ],\n" +
                        "                    \"creditamount\":\"\",\n" +
                        "                    \"debitamount\":\""+elm+"\",\n" +
                        "                    \"explanation\":\"收饿了么\",\n" +
                        "                    \"localcreditamount\":\"\",\n" +
                        "                    \"localdebitamount\":\"\",\n" +
                        "                    \"pk_accsubj\":\"100202\",\n" +
                        "                    \"pk_currtype\":\"CNY\"\n" +
                        "                }," +
                        "{\n" +
                        "\t\t\t\t\"ass\":[\n" +
                        "                        {\n" +
                        "                            \"checktypecode\":\"2\",\n" +
                        "                            \"checkvaluecode\":'"+yaoma+"'\n" +
                        "                        },\n" +
                        "\t\t\t\t\t\t{\n" +
                        "                            \"checktypecode\":\"7\",\n" +
                        "                            \"checkvaluecode\":\"0019\"\n" + //饿了么外卖编号
                        "                        }\n" +
                        "                    ],\n" +
                        "                    \"creditamount\":\""+elm+"\",\n" +
                        "                    \"creditquantity\":\"\",\n" +
                        "                    \"debitamount\":\"\",\n" +
                        "                    \"debitquantity\":\"\",\n" +
                        "                    \"detailindex\":\"\",\n" +
                        "                    \"excrate1\":\"\",\n" +
                        "                    \"explanation\":\"收饿了么\",\n" +
                        "                    \"localcreditamount\":\"\",\n" +
                        "                    \"localdebitamount\":\"\",\n" +
                        "                    \"pk_accsubj\":\"112212\",\n" +
                        "                    \"pk_currtype\":\"CNY\",\n" +
                        "                    \"price\":\"\"\n" +
                        "                },\n");
            }
            //美团买单金额大于0的话，生成美团买单的凭证
            if(mtmdFlag==1){
                if(mdsxfFlag==1){
                    sb.append("                {\n" +
                            "                    \"ass\":[\n" +
                            "                        {\n" +
                            "                            \"checktypecode\":\"2\",\n" +
                            "                            \"checkvaluecode\":'"+yaoma+"'\n" +
                            "                        }\n" +
                            "                    ],\n" +
                            "                    \"creditamount\":\"\",\n" +
                            "                    \"debitamount\":\""+mdsxf+"\",\n" +
                            "                    \"explanation\":\"买单手续费\",\n" +
                            "                    \"localcreditamount\":\"\",\n" +
                            "                    \"localdebitamount\":\"\",\n" +
                            "                    \"pk_accsubj\":\"660306\",\n" +
                            "                    \"pk_currtype\":\"CNY\"\n" +
                            "                }," );
                }
                sb.append( "{\n" +
                        "                    \"ass\":[\n" +
                        "                        {\n" +
                        "                            \"checktypecode\":\"2\",\n" +
                        "                            \"checkvaluecode\":'"+yaoma+"'\n" +
                        "                        },\n" +
                        "                        {\n" +
                        "                            \"checktypecode\":\"96\",\n" +
                        "                            \"checkvaluecode\":"+kh+"\n" +
                        "                        }\n" +
                        "                    ],\n" +
                        "                    \"creditamount\":\"\",\n" +
                        "                    \"debitamount\":\""+mtmd+"\",\n" +
                        "                    \"explanation\":\"收买单\",\n" +
                        "                    \"localcreditamount\":\"\",\n" +
                        "                    \"localdebitamount\":\"\",\n" +
                        "                    \"pk_accsubj\":\"100202\",\n" +
                        "                    \"pk_currtype\":\"CNY\"\n" +
                        "                },"+
                        "{\n" +
                        "\t\t\t\t\"ass\":[\n" +
                        "                        {\n" +
                        "                            \"checktypecode\":\"2\",\n" +
                        "                            \"checkvaluecode\":'"+yaoma+"'\n" +
                        "                        },\n" +
                        "\t\t\t\t\t\t{\n" +
                        "                            \"checktypecode\":\"7\",\n" +
                        "                            \"checkvaluecode\":\"0015\"\n" + //点评买单编号
                        "                        }\n" +
                        "                    ],\n" +
                        "                    \"creditamount\":\""+mdDf+"\",\n" +
                        "                    \"creditquantity\":\"\",\n" +
                        "                    \"debitamount\":\"\",\n" +
                        "                    \"debitquantity\":\"\",\n" +
                        "                    \"detailindex\":\"\",\n" +
                        "                    \"excrate1\":\"\",\n" +
                        "                    \"explanation\":\"收买单\",\n" +
                        "                    \"localcreditamount\":\"\",\n" +
                        "                    \"localdebitamount\":\"\",\n" +
                        "                    \"pk_accsubj\":\"112208\",\n" +
                        "                    \"pk_currtype\":\"CNY\",\n" +
                        "                    \"price\":\"\"\n" +
                        "                },\n");
            }
            //美团外卖
            if(mtwmFlag==1){
                sb.append("                {\n" +
                        "                    \"ass\":[\n" +
                        "                        {\n" +
                        "                            \"checktypecode\":\"2\",\n" +
                        "                            \"checkvaluecode\":'"+yaoma+"'\n" +
                        "                        },\n" +
                        "                        {\n" +
                        "                            \"checktypecode\":\"96\",\n" +
                        "                            \"checkvaluecode\":"+kh+"\n" +
                        "                        }\n" +
                        "                    ],\n" +
                        //借方科目
                        "                    \"creditamount\":\"\",\n" +
                        "                    \"debitamount\":\""+mtwm+"\",\n" +
                        "                    \"explanation\":\"收美团外卖\",\n" +
                        "                    \"localcreditamount\":\"\",\n" +
                        "                    \"localdebitamount\":\"\",\n" +
                        "                    \"pk_accsubj\":\"100202\",\n" +
                        "                    \"pk_currtype\":\"CNY\"\n" +
                        "                }," +
                        "{\n" +
                        //贷方辅助核算
                        "\t\t\t\t\"ass\":[\n" +
                        "                        {\n" +
                        "                            \"checktypecode\":\"2\",\n" +
                        "                            \"checkvaluecode\":'"+yaoma+"'\n" +
                        "                        },\n" +
                        "\t\t\t\t\t\t{\n" +
                        "                            \"checktypecode\":\"7\",\n" +
                        "                            \"checkvaluecode\":\"0035\"\n" + //美团外卖编号
                        "                        }\n" +
                        "                    ],\n" +
                        //贷方科目
                        "                    \"creditamount\":\""+mtwm+"\",\n" +
                        "                    \"creditquantity\":\"\",\n" +
                        "                    \"debitamount\":\"\",\n" +
                        "                    \"debitquantity\":\"\",\n" +
                        "                    \"detailindex\":\"\",\n" +
                        "                    \"excrate1\":\"\",\n" +
                        "                    \"explanation\":\"收美团外卖\",\n" +
                        "                    \"localcreditamount\":\"\",\n" +
                        "                    \"localdebitamount\":\"\",\n" +
                        "                    \"pk_accsubj\":\"112211\",\n" +
                        "                    \"pk_currtype\":\"CNY\",\n" +
                        "                    \"price\":\"\"\n" +
                        "                }\n");
            }
            sb.append("            ],\n" +
                    "            \"pk_corp\":"+bh+",\n" +
                    "            \"pk_glorgbook\":\""+bh+"-bty01\",\n" +
                    "            \"pk_prepared\":\"15253172037\",\n" +
                    "            \"pk_vouchertype\":\"记账\",\n" +
                    "            \"prepareddate\":"+lastDays+"\n" +
                    "        }\n" +
                    "    ]\n" +
                    "}");
            //System.out.println("sb:"+sb);
            HttpEntity<String> formEntity = new HttpEntity<String>(sb.toString(), headers);
            String url = "http://124.70.28.147:8088/u8cloud/api/gl/voucher/insert";
            String result = restTemplate.postForEntity(url, formEntity, String.class).getBody();
            System.out.println("result:"+result);
            JsonParser jp = new JsonParser();
            //将json字符串转化成json对象
            JsonObject jo = jp.parse(result).getAsJsonObject();
            //获取errcode对应的值
            String status = jo.get("status").getAsString();

            if(status.equals("falied")){
                String errormsg = jo.get("errormsg").getAsString();
                return  ResponseData.error(501,errormsg);
            }else{
                return ResponseData.success();
            }

    }


    /**
     * 银行账支出凭证
     */
    @RequestMapping(value = "/zcpz")
    @ResponseBody
    public ResponseData zcpz(YaoluBankFlowParam param){
        String beginTime = "";
        String endTime = "";
        String lastDay="";
        if (ToolUtil.isNotEmpty(param.getTimeLimit())) {
            String[] split = param.getTimeLimit().split(" - ");
            beginTime = split[0].replace("-","");
            endTime = split[1].replace("-","");
            lastDay=split[1];
            int month1 = Integer.valueOf(beginTime.substring(4,6));
            int month2 = Integer.valueOf(endTime.substring(4,6));
//            if(month1==month2){
//                //获取当前日期本月的最后一天
//                lastDay=DateUtil.getLastDayByMonth(split[1]);
//            }else {
//                //获取当前日期上个月的最后一天
//                lastDay = DateUtil.getLastDayByMonths(split[1]);
//            }
            param.setBeginTime(beginTime);
            param.setEndTime(endTime);
        }
        List<YaoluBankFlowResult> list = yaoluBankFlowService.dmList();
        for (YaoluBankFlowResult yybbResult : list) {
            String dm = yybbResult.getDm();
            String yaoma = yybbResult.getYaoma();
            String yhk = yybbResult.getKh();
            String bh = yybbResult.getBh();
            param.setDm(dm);

            BigDecimal zfbsxf = BigDecimal.ZERO;
            BigDecimal wxsxf = BigDecimal.ZERO;
            BigDecimal mdsxf = BigDecimal.ZERO;
            BigDecimal mtsxf = BigDecimal.ZERO;
            BigDecimal sxfsxf = BigDecimal.ZERO;

            List<YaoluBankFlowResult> list1 = yaoluBankFlowService.bankZcListByDm(param);
            //配送费
            BigDecimal psf=BigDecimal.ZERO;
            //美团抽点
            BigDecimal mtcd=BigDecimal.ZERO;
            //管理费
            BigDecimal glf=BigDecimal.ZERO;
            //品牌使用费
            BigDecimal ppsyf=BigDecimal.ZERO;

            for (YaoluBankFlowResult flowResult : list1) {
                String zy=flowResult.getZy()==null?"":flowResult.getZy();
                if(zy.contains("配送")){
                    psf=psf.add(flowResult.getZc());
                }
                if(zy.contains("美团提点")|zy.contains("美团抽点")){
                    mtcd=mtcd.add(flowResult.getZc());
                } if(zy.contains("管理费")){
                    glf=glf.add(flowResult.getZc());
                }
                if(zy.contains("服务费")){
                    ppsyf=ppsyf.add(flowResult.getZc());
                }
            }
            //配送费标志
            int psfFlag=psf.compareTo(BigDecimal.ZERO);
            //美团抽点标志
            int mtcdFlag=mtcd.compareTo(BigDecimal.ZERO);
            //管理费标志
            int glfFlag=glf.compareTo(BigDecimal.ZERO);
            //品牌使用费标志
            int ppsyfFlag=ppsyf.compareTo(BigDecimal.ZERO);


            RestTemplate restTemplate = new RestTemplate();
            HttpHeaders headers = new HttpHeaders();
            MediaType type = MediaType.parseMediaType("application/json; charset=UTF-8");
            headers.setContentType(type);
            headers.add("trantype", "code"); // 档案翻译方式，枚举值为：编码请录入 code， 名称请录入 name， 主键请录入 pk
            headers.add("system", "001"); // 系统编码
            headers.add("usercode", "15253172037"); // 用户
            headers.add("password", "514576f58a89767b46c1b44767a08f77");
            headers.add("Accept", MediaType.APPLICATION_JSON.toString());

            StringBuilder sb=new StringBuilder();
            sb.append("{\n" +
                    "    \"voucher\":[\n" +
                    "        {\n" +
                    "            \"details\":[");
            //配送费凭证
            if(psfFlag==1){
                sb.append("                {\n" +
                        "                    \"ass\":[\n" +
                        "                        {\n" +
                        "                            \"checktypecode\":\"2\",\n" +
                        "                            \"checkvaluecode\":'"+yaoma+"'\n" +
                        "                        },\n" +
                        "                        {\n" +
                        "                            \"checktypecode\":\"73\",\n" +
                        "                            \"checkvaluecode\":\"201\"\n" +//山东配送中心编码
                        "                        }\n" +
                        "                    ],\n" +
                        "                    \"creditamount\":\"\",\n" +
                        "                    \"debitamount\":\""+psf+"\",\n" +
                        "                    \"explanation\":\"付配送\",\n" +
                        "                    \"localcreditamount\":\"\",\n" +
                        "                    \"localdebitamount\":\"\",\n" +
                        "                    \"pk_accsubj\":\"2202\",\n" +
                        "                    \"pk_currtype\":\"CNY\"\n" +
                        "                }," +
                        "{\n" +
                        "\t\t\t\t\"ass\":[\n" +
                        "                        {\n" +
                        "                            \"checktypecode\":\"2\",\n" +
                        "                            \"checkvaluecode\":'"+yaoma+"'\n" +
                        "                        }\n" +
                        "                    ],\n" +
                        "                    \"creditamount\":\""+psf+"\",\n" +
                        "                    \"creditquantity\":\"\",\n" +
                        "                    \"debitamount\":\"\",\n" +
                        "                    \"debitquantity\":\"\",\n" +
                        "                    \"detailindex\":\"\",\n" +
                        "                    \"excrate1\":\"\",\n" +
                        "                    \"explanation\":\"付配送\",\n" +
                        "                    \"localcreditamount\":\"\",\n" +
                        "                    \"localdebitamount\":\"\",\n" +
                        "                    \"pk_accsubj\":\"100208\",\n" +
                        "                    \"pk_currtype\":\"CNY\",\n" +
                        "                    \"price\":\"\"\n" +
                        "                },\n");
            }
            //美团抽点凭证
            if(mtcdFlag==1){
                sb.append("                {\n" +
                        "                    \"ass\":[\n" +
                        "                        {\n" +
                        "                            \"checktypecode\":\"2\",\n" +
                        "                            \"checkvaluecode\":'"+yaoma+"'\n" +
                        "                        }\n" +
                        "                    ],\n" +
                        "                    \"creditamount\":\"\",\n" +
                        "                    \"debitamount\":\""+mtcd+"\",\n" +
                        "                    \"explanation\":\"付美团抽点\",\n" +
                        "                    \"localcreditamount\":\"\",\n" +
                        "                    \"localdebitamount\":\"\",\n" +
                        "                    \"pk_accsubj\":\"224103\",\n" +
                        "                    \"pk_currtype\":\"CNY\"\n" +
                        "                }," +
                        "{\n" +
                        "\t\t\t\t\"ass\":[\n" +
                        "                        {\n" +
                        "                            \"checktypecode\":\"2\",\n" +
                        "                            \"checkvaluecode\":'"+yaoma+"'\n" +
                        "                        }\n" +
                        "                    ],\n" +
                        "                    \"creditamount\":\""+mtcd+"\",\n" +
                        "                    \"creditquantity\":\"\",\n" +
                        "                    \"debitamount\":\"\",\n" +
                        "                    \"debitquantity\":\"\",\n" +
                        "                    \"detailindex\":\"\",\n" +
                        "                    \"excrate1\":\"\",\n" +
                        "                    \"explanation\":\"付美团抽点\",\n" +
                        "                    \"localcreditamount\":\"\",\n" +
                        "                    \"localdebitamount\":\"\",\n" +
                        "                    \"pk_accsubj\":\"100208\",\n" +
                        "                    \"pk_currtype\":\"CNY\",\n" +
                        "                    \"price\":\"\"\n" +
                        "                },\n");
            }
            //管理费凭证
            if(glfFlag==1){
                sb.append("                {\n" +
                        "                    \"ass\":[\n" +
                        "                        {\n" +
                        "                            \"checktypecode\":\"2\",\n" +
                        "                            \"checkvaluecode\":'"+yaoma+"'\n" +
                        "                        }\n" +
                        "                    ],\n" +
                        "                    \"creditamount\":\"\",\n" +
                        "                    \"debitamount\":\""+glf+"\",\n" +
                        "                    \"explanation\":\"付管理费\",\n" +
                        "                    \"localcreditamount\":\"\",\n" +
                        "                    \"localdebitamount\":\"\",\n" +
                        "                    \"pk_accsubj\":\"224102\",\n" +
                        "                    \"pk_currtype\":\"CNY\"\n" +
                        "                }," +
                        "{\n" +
                        "\t\t\t\t\"ass\":[\n" +
                        "                        {\n" +
                        "                            \"checktypecode\":\"2\",\n" +
                        "                            \"checkvaluecode\":'"+yaoma+"'\n" +
                        "                        }\n" +
                        "                    ],\n" +
                        "                    \"creditamount\":\""+glf+"\",\n" +
                        "                    \"creditquantity\":\"\",\n" +
                        "                    \"debitamount\":\"\",\n" +
                        "                    \"debitquantity\":\"\",\n" +
                        "                    \"detailindex\":\"\",\n" +
                        "                    \"excrate1\":\"\",\n" +
                        "                    \"explanation\":\"付管理费\",\n" +
                        "                    \"localcreditamount\":\"\",\n" +
                        "                    \"localdebitamount\":\"\",\n" +
                        "                    \"pk_accsubj\":\"100208\",\n" +
                        "                    \"pk_currtype\":\"CNY\",\n" +
                        "                    \"price\":\"\"\n" +
                        "                },\n");
            }
            //品牌使用费凭证
            if(ppsyfFlag==1){
                sb.append("                {\n" +
                        "                    \"ass\":[\n" +
                        "                        {\n" +
                        "                            \"checktypecode\":\"2\",\n" +
                        "                            \"checkvaluecode\":'"+yaoma+"'\n" +
                        "                        }\n" +
                        "                    ],\n" +
                        "                    \"creditamount\":\"\",\n" +
                        "                    \"debitamount\":\""+ppsyf+"\",\n" +
                        "                    \"explanation\":\"付品牌使用费\",\n" +
                        "                    \"localcreditamount\":\"\",\n" +
                        "                    \"localdebitamount\":\"\",\n" +
                        "                    \"pk_accsubj\":\"660126\",\n" +
                        "                    \"pk_currtype\":\"CNY\"\n" +
                        "                }," +
                        "{\n" +
                        "\t\t\t\t\"ass\":[\n" +
                        "                        {\n" +
                        "                            \"checktypecode\":\"2\",\n" +
                        "                            \"checkvaluecode\":'"+yaoma+"'\n" +
                        "                        }\n" +
                        "                    ],\n" +
                        "                    \"creditamount\":\""+ppsyf+"\",\n" +
                        "                    \"creditquantity\":\"\",\n" +
                        "                    \"debitamount\":\"\",\n" +
                        "                    \"debitquantity\":\"\",\n" +
                        "                    \"detailindex\":\"\",\n" +
                        "                    \"excrate1\":\"\",\n" +
                        "                    \"explanation\":\"付品牌使用费\",\n" +
                        "                    \"localcreditamount\":\"\",\n" +
                        "                    \"localdebitamount\":\"\",\n" +
                        "                    \"pk_accsubj\":\"100208\",\n" +
                        "                    \"pk_currtype\":\"CNY\",\n" +
                        "                    \"price\":\"\"\n" +
                        "                },\n");
            }
            sb.append("            ],\n" +
                    "            \"pk_corp\":"+bh+",\n" +
                    "            \"pk_glorgbook\":\""+bh+"-bty01\",\n" +
                    "            \"pk_prepared\":\"15253172037\",\n" +
                    "            \"pk_vouchertype\":\"记账\",\n" +
                    "            \"prepareddate\":"+lastDay+"\n" +
                    "        }\n" +
                    "    ]\n" +
                    "}");
            System.out.println("sb:"+sb.toString());
            HttpEntity<String> formEntity = new HttpEntity<String>(sb.toString(), headers);
            String url = "http://124.70.28.147:8089/u8cloud/api/gl/voucher/insert";
            String result = restTemplate.postForEntity(url, formEntity, String.class).getBody();
            System.out.println("result:"+result);
            JsonParser jp = new JsonParser();
            //将json字符串转化成json对象
            JsonObject jo = jp.parse(result).getAsJsonObject();
            //获取errcode对应的值
            String status = jo.get("status").getAsString();
            if (status.equals("falied")) {
                String errormsg = jo.get("errormsg").getAsString();
                List<YaoluBankFlow> list2 = bankFlowMapper.selectList(new QueryWrapper<YaoluBankFlow>().eq("dm", dm));
                if (list2.size() > 0) {
                    YaoluBankFlowParam param1 = new YaoluBankFlowParam();
                    param1.setDm(dm);
                    param1.setErrormsg(errormsg);
                    bankFlowMapper.updateZcErrorMsgByDm(param1);
                }
                //
                //System.out.println("errormsg:"+errormsg);
            } else {
                List<YaoluBankFlow> list2 = bankFlowMapper.selectList(new QueryWrapper<YaoluBankFlow>().eq("dm", dm));
                if (list2.size() > 0) {
                    YaoluBankFlowParam param1 = new YaoluBankFlowParam();
                    param1.setDm(dm);
                    bankFlowMapper.updateZcByDm(param1);
                }
                //System.out.println("凭证生成成功！");
            }
            /////
        }

        return  ResponseData.success();
    }

}


